/**Circular Sequence, ACM/ICPC Seoul 2004, UVa1584
 * Find the lexicographically smallest sequence.
 */
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
char now[maxn], ans[maxn];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int T;
  cin >> T;
  while(T--) {
    cin >> now;
    strcpy(ans, now);
    size_t len = strlen(now);
    for(size_t i=0; i < len; ++i) {
      if(strcmp(now, ans)<0) {
        strcpy(ans, now);
      }
      char tmp = now[0];
      memmove(now, now+1, len-1);
      now[len-1]=tmp;
    }
    cout << ans << endl;
  }
  return 0;
}
